//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#ifndef GADGET_H
#define GADGET_H

#include "psys.h"
#include "psys_stdgfx.h"
#include "pwindow.h"
#include "guibase.h"
#include "image_base.h"

//==================================================================
#define GGET_EDIT_MAX_CHARS		256

extern float	_gget_frame_color[3];
extern float	_gget_text_color_active[3];
extern float	_gget_text_color_nonactive[3];

//==================================================================
enum GGET_TYPE
{
	GGET_TYPE_STATICTXT,
	GGET_TYPE_BUTTON,
	GGET_TYPE_TAB,
	GGET_TYPE_CHECK,
	GGET_TYPE_EDIT,
};

//==================================================================
enum gget_flag_o
{
	GGET_FLG_BUTTON_TOGGLED_O = 0,
	GGET_FLG_ACTIVE_O,
	GGET_FLG_DISABLED_O,
	GGET_FLG_INVISIBLE_O,
	GGET_FLG_REMOVED_O,

	GGET_FLG_ALIGN_LEFT_O,
	GGET_FLG_ALIGN_RIGHT_O,
	GGET_FLG_ALIGN_TOP_O,
	GGET_FLG_ALIGN_BOTTOM_O,

	GGET_FLG_MOUSEOVER_O,
	GGET_FLG_PRESSED_O,

	GGET_FLG_CLOSE_TAB_BUTT_O,
};

//==================================================================
enum gget_flag
{
	GGET_FLG_BUTTON_TOGGLED	= 1 << GGET_FLG_BUTTON_TOGGLED_O,
	GGET_FLG_ACTIVE			= 1 << GGET_FLG_ACTIVE_O,			// item has been selected and receives keyboard input
	GGET_FLG_DISABLED		= 1 << GGET_FLG_DISABLED_O,			// item can't be interacted with
	GGET_FLG_INVISIBLE		= 1 << GGET_FLG_INVISIBLE_O,		// item is not visible (nor can be interacted with)
	GGET_FLG_REMOVED		= 1 << GGET_FLG_REMOVED_O,

	GGET_FLG_ALIGN_LEFT		= 1 << GGET_FLG_ALIGN_LEFT_O,
	GGET_FLG_ALIGN_RIGHT	= 1 << GGET_FLG_ALIGN_RIGHT_O,
	GGET_FLG_ALIGN_TOP		= 1 << GGET_FLG_ALIGN_TOP_O,
	GGET_FLG_ALIGN_BOTTOM	= 1 << GGET_FLG_ALIGN_BOTTOM_O,
							  
	GGET_FLG_MOUSEOVER		= 1 << GGET_FLG_MOUSEOVER_O,
	GGET_FLG_PRESSED		= 1 << GGET_FLG_PRESSED_O,

	GGET_FLG_CLOSE_TAB_BUTT = 1 << GGET_FLG_CLOSE_TAB_BUTT_O,

	GGET_FLG_ALIGN_MASK		= GGET_FLG_ALIGN_LEFT		|
							GGET_FLG_ALIGN_RIGHT	|
							GGET_FLG_ALIGN_TOP		|
							GGET_FLG_ALIGN_BOTTOM
};



//==================================================================
typedef enum
{
	GGET_DEF_FLG_BUTTON_TOGGLED	= GGET_FLG_BUTTON_TOGGLED,
	GGET_DEF_FLG_ACTIVE			= GGET_FLG_ACTIVE,
	GGET_DEF_FLG_DISABLED		= GGET_FLG_DISABLED,
//	GGET_DEF_FLG_SELECTED		= GGET_FLG_SELECTED,
	GGET_DEF_FLG_INVISIBLE		= GGET_FLG_INVISIBLE,

	GGET_DEF_FLG_ALIGN_LEFT		= GGET_FLG_ALIGN_LEFT,
	GGET_DEF_FLG_ALIGN_RIGHT	= GGET_FLG_ALIGN_RIGHT,
	GGET_DEF_FLG_ALIGN_TOP		= GGET_FLG_ALIGN_TOP,
	GGET_DEF_FLG_ALIGN_BOTTOM	= GGET_FLG_ALIGN_BOTTOM,
} gget_def_flag;

//==================================================================
class GGET_Manager;

//==================================================================
class GGET_Item
{
	friend class GGET_Button;
	friend class GGET_Tab;

public:
	enum StdIcon
	{
		STD_ICO_NONE,
		STD_ICO_ON,
		STD_ICO_OFF,

		STD_ICO_MARK_OFF,
		STD_ICO_MARK_ON,
	};

public:
	int					_id;
	GGET_TYPE			_type;		// GGET_TYPE
	PSYS::RectF			_rect;
	u_int				_flags;		// gget_flag
	TCHAR				_text[64];
	PSYSGFX::Color4f	_text_col;
	StdIcon				_ico_id;
	PUtils::ImageBase	*_ico_imgp;

private:
	GGET_Manager	*_managerp;

public:
	GGET_Item( GGET_Manager *managerp, GGET_TYPE type );
	virtual ~GGET_Item(){};

	void			Show( bool onoff );
	void			SetRect( float x, float y, float w, float h );
	void			SetPos( float x, float y );
	void			SetSize( float w, float h );
	float			GetWidth() const
	{
		return _rect._w;
	}
	float			GetHeight() const
	{
		return _rect._h;
	}
	void			SetText( const TCHAR *txtp );
	const TCHAR		*GetText();
	void			SetTextColor( float r, float g, float b, float a );
	void			SetIcon( StdIcon ico_id );
	void			SetIcon( PUtils::ImageBase *imagep );
	GGET_TYPE		GetType() { return _type; }
	virtual	void	Paint()=0;

	GGET_Manager	&GetManager()
	{
		return *_managerp;
	}

private:
	void			drawIconButt( PSYS::RectF rect );
};

//==================================================================
enum GGET_CB_Action 
{
	GGET_CB_ACTION_CLICK,
	GGET_CB_ACTION_KEYCHAR,
	GGET_CB_ACTION_CLOSETAB,
};

typedef void (*GGET_Callback)( void *userdatap, int gget_id, GGET_Item *itemp, GGET_CB_Action action );

//==================================================================
class GGET_StaticText : public GGET_Item
{
public:
	enum FillType
	{
		FILL_TYPE_NONE = 0,
		FILL_TYPE_HTOOLBAR
	};
	int		_fill_type;

	GGET_StaticText( GGET_Manager *managerp ) :
		GGET_Item( managerp, GGET_TYPE_STATICTXT ),
		_fill_type(FILL_TYPE_NONE)
	{
	}
	~GGET_StaticText(){}

	void SetFillType( FillType fill_type )
	{
		_fill_type = fill_type;
	}

	void	Paint();
};

//==================================================================
class GGET_Button : public GGET_Item
{
public:
	GGET_Button( GGET_Manager *managerp ) : GGET_Item( managerp, GGET_TYPE_BUTTON )
	{
	}
	~GGET_Button(){}

	void	Paint();
};

//==================================================================
class GGET_Tab : public GGET_Item
{
public:
	GGET_Button		*_close_buttp;

	GGET_Tab( GGET_Manager *managerp ) :
		GGET_Item( managerp, GGET_TYPE_TAB ),
		_close_buttp(NULL)
	{
		_flags &= ~GGET_FLG_ALIGN_MASK;
		_flags |= GGET_FLG_ALIGN_LEFT;
	}
	~GGET_Tab(){}

	void	Paint();

	void	CalcButtFrame();
};

//==================================================================
class GGET_CheckBox : public GGET_Item
{
public:
	GGET_CheckBox( GGET_Manager *managerp ) : GGET_Item( managerp, GGET_TYPE_CHECK )
	{
	}
	~GGET_CheckBox(){}

	void	Paint();
};

//==================================================================
class GGET_EditBox : public GGET_Item
{
public:
	TCHAR		_line_txt[GGET_EDIT_MAX_CHARS];
	int			_cursor_pos;

	GGET_EditBox( GGET_Manager *managerp ) : GGET_Item( managerp, GGET_TYPE_EDIT )
	{
	}
	~GGET_EditBox(){}

	void	Paint();
	bool	OnKeydown( class WindowEvent *eventp );
	bool	OnKeychar( void *userdatap, class WindowEvent *eventp, GGET_Callback cback );
};

//==================================================================
void gget_calc_size( GGET_TYPE type, const TCHAR *txtp, int out_size[2] );

//==================================================================
void gget_int_set( GGET_Item *ggp, int val );
void gget_float_set( GGET_Item *ggp, float val );
float gget_float_range_get( GGET_Item *ggp, float minval, float maxval );
int gget_int_range_get( GGET_Item *ggp, int minval, int maxval );

#include "gadget_manager.h"

#endif
